package com.guli.file.controller;

import com.aliyun.oss.OSS;
import com.guli.common.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("file/upload")
public class OSSUploadController {

    @Value("${oss.hostName}")
    private String hostName;

    @Value("${oss.bucket}")
    private String bucketName;

    @Autowired
    private OSS client;

    @PostMapping("/oss")
    public Result ossUpload(@RequestParam("file")MultipartFile file){
        try {
            //文件名字
            String filename = file.getOriginalFilename();
            client.putObject(bucketName, filename, file.getInputStream());
            return new Result(true, "100",hostName+filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false, "000","上传失败");
    }

}
